package com.adnaan.backend.common.handler;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动填充处理器
 * author: adnaan
 */
@Component
@Slf4j
public class AutoFillHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        // 自动填充创建时间
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        // 自动填充创建人
        if (!StpUtil.isLogin()) {
            this.strictInsertFill(metaObject, "createBy", String.class, "游客");
            return;
        }
        this.strictInsertFill(metaObject, "createBy", String.class, StpUtil.getLoginIdAsString());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    }
}